package com.gogowan.agroenterprise.widget.xlistview;

import com.gogowan.agroenterprise.utils.PalUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @Description: 加载布局
 * @author mdp
 * @date 2017年1月3日 上午10:58:58 
 */
public class LoadingLayout extends LinearLayout {

    private static final String LOADING = "正在加载...";
    private static final String LOAD_ERROR = "加载失败，点击刷新";
    private static final String LOAD_NULL = "暂无内容，点击刷新";
    private static final int TEXT_COLOR = 0xff666666;
    private static final int TEXT_SIZE = 16;
    private static final int PROGRESSBAR_DIMEN = 30;

    private TextView textView;
    private ProgressBar progressBar;
    private View replacedView;

    public LoadingLayout(Context context) {
        this(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setVisibility(GONE);
        initView(context);
    }

    private void initView(Context context) {
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleInverse);
        textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setText(LOADING);
        textView.setTextColor(TEXT_COLOR);
        textView.setTextSize(TEXT_SIZE);
        addView(progressBar, new LayoutParams(PalUtils.dp2px(context, PROGRESSBAR_DIMEN), PalUtils.dp2px(context, PROGRESSBAR_DIMEN)));
        addView(textView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    /**
     * 设置操作界面(必须调用)
     *
     * @param replacedView 被替换的布局
     */
    public void setReplacedView(View replacedView) {
        this.replacedView = replacedView;
    }

    /**
     * 显示操作页面
     */
    public void hide() {
        setVisibility(View.GONE);
        replacedView.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载页面
     */
    public void show() {
        setVisibility(View.VISIBLE);
        replacedView.setVisibility(View.GONE);
    }

    /**
     * 显示加载中页面
     */
    public void showLoading() {
        show();
        progressBar.setVisibility(View.VISIBLE);
        textView.setText(LOADING);
    }

    /**
     * 显示查询加载页面
     */
    public void showNull(final OnNullClickListener onNullClickListener) {
        show();
        progressBar.setVisibility(View.GONE);
        textView.setText(LOAD_NULL);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                textView.setText(LOADING);
                onNullClickListener.onClick();
            }
        });
    }

    /**
     * 显示加载失败页面
     */
    public void showReload(final OnReloadClickListener onReloadClickListener) {
        show();
        progressBar.setVisibility(View.GONE);
        textView.setText(LOAD_ERROR);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                textView.setText(LOADING);
                onReloadClickListener.onClick();
            }
        });
    }

    /**
     * 无结果时的点击事件
     */
    public interface OnNullClickListener {
        void onClick();
    }

    /**
     * 加载失败时的点击事件
     */
    public interface OnReloadClickListener {
        void onClick();
    }
}
